#ifndef DATABASE_INCLUDED
#define DATABASE_INCLUDED

#include <sqlite3.h>

#include "Typedef.h"
#include "Book.h"
#include "Log.h"
#include "Typedef.h"

class Book;

class Database
{
public:
    Database(){};
    virtual ~Database(){};

public:
    virtual BPcode CheckTables(){return BP_BAD;};
    virtual BPcode InitTables(){return BP_BAD;};
    virtual BPcode DeleteTables(){return BP_BAD;};

    virtual BPcode OpenDatabase(){return BP_BAD;};
    virtual BPcode CloseDatabase(){return BP_BAD;};
    virtual BPcode CheckDatabase(){return BP_BAD;};
};

class DBSqlite : public Database
{
public:
    DBSqlite(Bpchar DatabasePath, Bpchar InitScriptPath);
    ~DBSqlite();

    uint32_t GetBookCount(string Keyword);
    uint32_t RefreshBookInfoList(uint32_t Page, BookInfo* InfoPtr, uint32_t Quantity, string Keyword);

    Book* GetBookByID(uint32_t BookID);
    BPcode AddBook(Book* book);

    BPcode SetAllBooksNonExisted();
    Bbool CheckIfBookExistsByDigestHex(string DigestHex);
    Bbool SetBookAsExistedByDigestHex(string DigestHex);

    string GetSearchDirectory();
    BPcode SetSearchDirectory(string directory);

    BPcode GetHistorysByBookID(uint32_t BookID, list<History*>* HList);
    BPcode AddHistory(History* history, uint32_t BookID);
    BPcode DeleteHistory(uint32_t HistoryID);

private:
    DBSqlite();

public:
    virtual BPcode CheckTables();
    virtual BPcode InitTables();
    virtual BPcode DeleteTables();

private:
    virtual BPcode OpenDatabase();
    virtual BPcode CloseDatabase();
    virtual BPcode CheckDatabase();

    enum
    {
        DatabaseOpen,
        DatabaseClosed
    } DatabaseStatus;

private:
    sqlite3* Handle;
    string DatabaseFilePath;
    string DatabaseInitScriptPath;
};

#endif // DATABASE_INCLUDED
